<?php

function list_jamsostek(){
	
	$header = array(
		'Nomor KPJ'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Nomor NIK'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Nama Tenaga Kerja'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Tgl Lahir'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Data Upah'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Iuran JKK'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Iuran JKM'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Iuran JPK'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Iuran JHT TK'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Iuran JHT Perusahaan'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Total Iuran'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		);
	
	$query = "SELECT * FROM karyawan 
		WHERE karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
		AND karyawan_id NOT IN (select karyawan_id FROM pemberhentian)
		ORDER BY karyawan_id DESC 		
	 ";

	$result = my_query($query );
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =40;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&field={$_GET['field']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array();  
	while($ey = my_fetch_array($result) ){
		$karyawan_id = $ey['karyawan_id'] ;
		$karyawan = loaddata_karyawan($karyawan_id); 
		$jkk=$jkm=$jpk=$jht_tk=$jht_per=$iuran=$total=0 ;
		
		$query = " SELECT b.jamsostek_id,a.kalkulasi FROM pr_gaji_komponen a
			INNER JOIN jamsostek_link b ON b.komponen_id=a.komponen_id
			WHERE a.periode_id=0
			AND a.karyawan_id={$karyawan_id}"
		;
		$result1 = my_query($query);	
		$jams=array();
		while($ey1 = my_fetch_array($result1) ){
			if($ey1['jamsostek_id']==4){
				$jams[$ey1['jamsostek_id']] = $ey1['kalkulasi']*-1 ;
			}else{
				$jams[$ey1['jamsostek_id']] = $ey1['kalkulasi'] ;
			}	
		}	
		$row[] = array(
			'kpj'=> $karyawan['jamsostek']  ,   
			'nik'=> $karyawan['nik']  ,   
			'nama'=>$karyawan['nama_gelar']  ,  
			'tgl'=> DATE('d-m-Y', strtotime($karyawan['tanggal_lahir'])), 
			'upah'=>position_text_align (rp_format($karyawan['gapok_km'] ) ,'right'),    
			'jkk'=> position_text_align (rp_format($jams[1]) ,'right'),    
			'jkm'=> position_text_align (rp_format($jams[2]) ,'right'),    
			'jpk'=> position_text_align (rp_format($jams[3]) ,'right'),    
			'jht_tk'=> position_text_align (rp_format($jams[4]) ,'right'),    
			'jht_per'=> position_text_align (rp_format($jams[5]) ,'right'),    
			'iuran'=> position_text_align (rp_format($jams[1]+$jams[2]+$jams[3]+$jams[4]+$jams[5]) ,'right'),    
		);
	}	
	$datas = table_rows($row);
	$view  = form_header( "..." , "..."  );
	$view .= form_field_display( '<span class="label_form"> '.get_tanggal_by_periode_id().'</span>'   , "<b>Periode</b>"    ); 
	
	$menuontop = array(
		'Cetak' => array('onclick'=>'javascript:window.open(\'index.php?com='.$_GET['com'].'&task=cetak\',\'mywin\',\'left=20,top=20,width=800,height=600,toolbar=0,resizable=0\');'),
		'Excel'=>array('onclick'=>'javascript:location.href=\'index.php?com='.$_GET['com'].'&task=excel\';'),
	);
	$printmenu = additional_menu_on_list( $menuontop ,'');
	$paging = $kgPagerOBJ ->showPaging();	
	return $view.$printmenu.table_builder($header , $datas ,  5 , false , $paging );
}

function get_tanggal_by_periode_id(){
	$periode_id =get_periode_gaji();
	$periodes = my_get_data_by_id('pr_periode','periode_id',$periode_id);
	$bulan = (int) date('m' , strtotime($periodes['tanggal']));
	$tahun = $periodes['tahun'] ;
	return sprintf( '%02d',$bulan).'-'.$tahun ;
}


function excel(){
	$header = array(
		'Nomor KPJ'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Nomor NIK'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Nama Tenaga Kerja'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Tgl Lahir'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Data Upah'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Iuran JKK'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Iuran JKM'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Iuran JPK'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Iuran JHT TK'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Iuran JHT Perusahaan'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Total Iuran'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		);
	
	$query = "SELECT * FROM karyawan 
		WHERE karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
		AND karyawan_id NOT IN (select karyawan_id FROM pemberhentian)
		ORDER BY karyawan_id DESC 		
	 ";
	$result = my_query($query );

	$row = array(); 
	$i = 0;  
	while($ey = my_fetch_array($result)){
		$i++;
		$karyawan_id = $ey['karyawan_id'] ;
		$karyawan = loaddata_karyawan($karyawan_id); 
		$jkk=$jkm=$jpk=$jht_tk=$jht_per=$iuran=$total=0 ;
		
		$query = " SELECT b.jamsostek_id,a.kalkulasi FROM pr_gaji_komponen a
			INNER JOIN jamsostek_link b ON b.komponen_id=a.komponen_id
			WHERE a.periode_id=0
			AND a.karyawan_id={$karyawan_id}"
		;
		$result1 = my_query($query);	
		$jams=array();
		while($ey1 = my_fetch_array($result1) ){
			if($ey1['jamsostek_id']==4){
				$jams[$ey1['jamsostek_id']] = $ey1['kalkulasi']*-1 ;
			}else{
				$jams[$ey1['jamsostek_id']] = $ey1['kalkulasi'] ;
			}	
		}	
		$row[] = array(
			'kpj'=> $karyawan['jamsostek']  ,   
			'nik'=> $karyawan['nik']  ,   
			'nama'=>$karyawan['nama_gelar']  ,  
			'tgl'=> DATE('d-m-Y', strtotime($karyawan['tanggal_lahir'])), 
			'upah'=>position_text_align (rp_format($karyawan['gapok_km'] ) ,'right'),    
			'jkk'=> position_text_align (rp_format($jams[1]) ,'right'),    
			'jkm'=> position_text_align (rp_format($jams[2]) ,'right'),    
			'jpk'=> position_text_align (rp_format($jams[3]) ,'right'),    
			'jht_tk'=> position_text_align (rp_format($jams[4]) ,'right'),    
			'jht_per'=> position_text_align (rp_format($jams[5]) ,'right'),    
			'iuran'=> position_text_align (rp_format($jams[1]+$jams[2]+$jams[3]+$jams[4]+$jams[5]) ,'right'),    
		);
	}
	$datas = table_rows_excel($row); 
	return table_builder_excel($header , $datas , 15 ,false ); 
}

function cetak(){
	$query = "SELECT * FROM karyawan 
		WHERE karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
		AND karyawan_id NOT IN (select karyawan_id FROM pemberhentian)
		ORDER BY karyawan_id DESC 		
	 ";
	$result = my_query($query );

	$datas=array();
	$datas[] = array(
		 'k1'=>array("Nomor KPJ" ,10,STR_PAD_BOTH),
		 'k2'=>array("Nomor NIK" ,10,STR_PAD_BOTH),
		 'k3'=>array("Nama Tenaga Kerja" ,10,STR_PAD_RIGHT),
		 'k4'=>array("Tgl Lahir" ,10,STR_PAD_LEFT),  
		 'k5'=>array("Data Upah" ,10,STR_PAD_LEFT),  
		 'k6'=>array("Iuran JKK" ,10,STR_PAD_LEFT),  
		 'k7'=>array("Iuran JKM" ,10,STR_PAD_LEFT),  
		 'k8'=>array("Iuran JPK" ,10,STR_PAD_LEFT),  
		 'k9'=>array("Iuran JHT TK" ,10,STR_PAD_LEFT),  
		 'k10'=>array("Iuran JHT Perusahaan" ,10,STR_PAD_LEFT),  
		 'k11'=>array("Total Iuran" ,10,STR_PAD_LEFT),  
	); 
	$datas[] = array(
		 'k1'=>array(" " ,100 ,STR_PAD_BOTH),
	); 

	$i=0;
	while($ey = my_fetch_array($result)){
		$i++;
		$karyawan_id = $ey['karyawan_id'] ;
		$karyawan = loaddata_karyawan($karyawan_id); 
		$jkk=$jkm=$jpk=$jht_tk=$jht_per=$iuran=$total=0 ;
		
		$query = " SELECT b.jamsostek_id,a.kalkulasi FROM pr_gaji_komponen a
			INNER JOIN jamsostek_link b ON b.komponen_id=a.komponen_id
			WHERE a.periode_id=0
			AND a.karyawan_id={$karyawan_id}"
		;
		$result1 = my_query($query);	
		$jams=array();
		while($ey1 = my_fetch_array($result1) ){
			if($ey1['jamsostek_id']==4){
				$jams[$ey1['jamsostek_id']] = $ey1['kalkulasi']*-1 ;
			}else{
				$jams[$ey1['jamsostek_id']] = $ey1['kalkulasi'] ;
			}	
		}	
		$i++;
		$karyawan = loaddata_karyawan($ey['karyawan_id']); 
		$datas[] = array(
			'k1'=>array($karyawan['jamsostek'] ,10 ,STR_PAD_BOTH),
			'k2'=>array($karyawan['nik'] ,10,STR_PAD_RIGHT),
			'k3'=>array($karyawan['nama_gelar'] ,10,STR_PAD_RIGHT),
			'k4'=>array(DATE('d-m-Y', strtotime($karyawan['tanggal_lahir'])) ,10,STR_PAD_LEFT),  
			'k5'=>array(rp_format($karyawan['gapok_km']) ,10,STR_PAD_LEFT),  
			'k6'=>array(rp_format($jams[1]) ,10,STR_PAD_LEFT),  
			'k7'=>array(rp_format($jams[2]) ,10,STR_PAD_LEFT),  
			'k8'=>array(rp_format($jams[3]) ,10,STR_PAD_LEFT),  
			'k9'=>array(rp_format($jams[4]) ,10,STR_PAD_LEFT),  
			'k10'=>array(rp_format($jams[5]) ,10,STR_PAD_LEFT),  
			'k11'=>array(rp_format($jams[1]+$jams[2]+$jams[3]+$jams[4]+$jams[5]) ,10,STR_PAD_LEFT),  
		); 
	}
	$view = 	str_pad("RINCIAN IURAN TENAGA KERJA",100," ",STR_PAD_BOTH);
	return $view ."\n"."\n".set_txt_report($datas);
}

